package com.huiquan.framework.base;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.ibatis.sqlmap.client.SqlMapClient;

public class EnhancedBaseDao<T> extends BaseDao {

	@Resource(name = "sqlMapClient")
	private SqlMapClient sqlMapClient;

	/** 命名空间名称. */
	private String space;

	public EnhancedBaseDao(String space) {
		this.space = space;
	}

	public Long insert(T entity) {
		return (Long) insert(space + "INSERT", entity, sqlMapClient);
	}

	public int update(T entity) {
		return update(space + "UPDATE", entity, sqlMapClient);
	}

	public int delete(T entity) {
		int returnInt = delete(space + "DELETE", entity, sqlMapClient);
		return returnInt;
	}

	public int deleteByMap(Map<String, Object> param) {
		int returnInt = delete(space + "DELETE_BY_MAP", param, sqlMapClient);
		return returnInt;
	}

	public int deleteById(Long id) {
		int returnInt = delete(space + "DELETE_BY_ID", id, sqlMapClient);
		return returnInt;
	}

	/**
	 * 根据id获取对象
	 * 
	 * @param id
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public T retrieveObjectById(Long id) {
		Map<String, Object> param = new HashMap<>();
		param.put("id", id);
		return (T) this.object(space + "RETRIEVE_OBJECT_BY_ID", param, sqlMapClient);
	}

	/**
	 * 自定义参数获取对象
	 * 
	 * @param param
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public T retrieveObjectByMap(Map<String, Object> param) {
		return (T) this.object(space + "RETRIEVE_OBJECT_BY_MAP", param, sqlMapClient);
	}

	/**
	 * 获取数据总数
	 * 
	 * @return
	 */
	public int retrieveSize() {
		Integer size = (Integer) this.object(space + "RETRIEVE_SIZE", sqlMapClient);
		return size == null ? 0 : size;
	}

	/**
	 * 获取所有的列表
	 * 
	 * @return
	 */
	public List<T> retrieveList() {
		List<T> result = retrieveList(new HashMap<String, Object>());
		return result;
	}

	/**
	 * 根据开始序号来获取列表
	 * 
	 * @param start
	 * @return
	 */
	public List<T> retrieveList(Integer start) {
		Map<String, Object> param = new HashMap<>();
		param.put("start", start);
		return retrieveList(param);
	}

	/**
	 * 根据关键词获取数据量
	 * 
	 * @param keyword
	 * @return
	 */
	public int retrieveSize(String keyword) {
		Map<String, Object> param = new HashMap<>();
		param.put("keyword", keyword);
		return retrieveSize(param);
	}

	/**
	 * 根据一个Map自定义获取满足条件的数量
	 * 
	 * @param param
	 * @return
	 */
	public int retrieveSize(Map<String, Object> param) {
		Integer size = (Integer) this.object(space + "RETRIEVE_SIZE", param, sqlMapClient);
		return size == null ? 0 : size;
	}

	/**
	 * 根据开始序号和关键字查询列表
	 * 
	 * @param start
	 * @param keyword
	 * @return
	 */
	public List<T> retrieveList(int start, String keyword) {
		Map<String, Object> param = new HashMap<>();
		param.put("start", start);
		param.put("keyword", keyword);
		return retrieveList(param);
	}

	/**
	 * 根据一个Map自定义获取列表
	 * 
	 * @param param
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<T> retrieveList(Map<String, Object> param) {
		return (List<T>) this.list(space + "RETRIEVE_LIST", param, sqlMapClient);
	}

}
